package com.winit.openapi.wh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.inbound.WHInboundOrderTrackingService;
import com.winit.oms.spi.inbound.command.WHQueryInboundTrackingCommand;
import com.winit.oms.spi.inbound.vo.InboundOrderTrackingVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.builder.InboundOrderBuilder;

/**
 * 入库单轨迹
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年12月22日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/tracking")
public class InboundOrderTrackingController extends BaseController {

    @Resource
    private WHInboundOrderTrackingService wHInboundOrderTrackingService;

    @RequestMapping(value = "/queryOrderTracking")
    public String queryOrderDetails(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        String orderNo = "";
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            orderNo = json.getString("orderNo");
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 查询订单轨迹
        WHQueryInboundTrackingCommand command = new WHQueryInboundTrackingCommand();
        command.setOrderNo(orderNo);
        List<InboundOrderTrackingVo> tList = wHInboundOrderTrackingService.queryTrackingList(command);
        Map<String, Object> map = new HashMap<String, Object>();
        if (tList != null && tList.size() > 0) {
            List<Map<String, Object>> dataList = InboundOrderBuilder.buildInboundOrderTrackingList(tList);
            map.put("trackingList", dataList);
        }
        responseMsg.setData(map);
        return SUCCESS;
    }

}
